package com.baizhi.controller;

import com.alibaba.fastjson.JSONObject;
import com.baizhi.entity.Product;
import com.baizhi.feignclient.ProductClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("category")
public class CategoryController {
    private static final Logger log = LoggerFactory.getLogger(CategoryController.class);

    @Autowired
    private ProductClient productClient;

    @GetMapping
    public String category() {
        log.info("category server ...");
        // 调用商品服务
        String product = productClient.product();
        String list = productClient.list();
        log.info("结果1：{}", product);
        log.info("结果2：{}", list);
        return "category ok !!!" + product;
    }

    @GetMapping("/test")
    public String test() {
        log.info("category server ...");
        // String result = productClient.test("小陈", 23);
        // String result = productClient.test1(21, "小明");
        // String result = productClient.test2(new Product(1, "超短裙", 23.23, new Date()));
        // String result = productClient.test3(new Product(1, "超短裙", 23.23, new Date()));
        // String result = productClient.test4(new String[]{"21", "23", "24"});
        String result = productClient.test5(Arrays.asList("21", "22", "23"));
        return "category ok !!!" + result;
    }

    @GetMapping("/getProduct")
    public Product getProduct() {
        log.info("category server ...");
        Product product = productClient.getProduct(21);
        log.info("product:{}", product);
        return product;
    }

    @GetMapping("/findByCategoryId")
    public List<Product> findByCategoryId() {
        log.info("category server ...");
        List<Product> products = productClient.findByCategoryId(1);
        products.forEach(product -> log.info("product:{}", product));
        return products;
    }

    @GetMapping("/findPageByCategoryId")
    public String findPageByCategoryId() {
        log.info("category server ...");
//        Map<String, Object> objectMap = productClient.findPageByCategoryId(1, 5, 1);
//        List<Product> data = (List<Product>) objectMap.get("data");
//        data.forEach(product -> log.info("product:{}", product));
//        Object total = objectMap.get("total");
//        log.info("total:{}", total);
        String result = productClient.findPageByCategoryId(1, 5, 1);
        // 自定义json反序列化
        JSONObject jsonObject = JSONObject.parseObject(result);
        List<Product> products = JSONObject.parseArray(jsonObject.get("data").toString(), Product.class);
        products.forEach(product -> log.info("product:{}", product));
        log.info("total:{}", jsonObject.get("total"));
        return result;
    }
}
